package user

import (
	"context"
	"github.com/jinzhu/copier"
	"github.com/zeromicro/go-zero/core/logx"
	"go.uber.org/zap"
	"navi-mumbai/app/api/business/internal/svc"
	"navi-mumbai/app/api/business/internal/types"
	"navi-mumbai/app/rpc/usercenter/pb"
	"strings"
	"time"
)

var logger = logx.WithDuration(time.Second)

var cacheMumbaiUserAccountSmsVerifyCodePhoneNoPrefix = "cache:mumbai:userAccount:smsVerifyCode:phoneNo"

type FbUserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFbUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FbUserLoginLogic {
	return &FbUserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FbUserLoginLogic) FbUserLogin(req *types.FbUserLoginReq, ip string, fbToken string) (resp *types.UserLoginResp, err error) {
	var in pb.FbUserLoginReq
	in.FbToken = fbToken
	in.AppVer = req.AppVer
	in.UserChannel = req.UserChannel
	in.Device = req.Device
	in.DeviceId = req.DeviceId
	in.AppsFlyerUID = req.AppsFlyerUID
	in.Pid = req.Pid
	if ip != "" {
		ips := strings.Split(ip, ",")
		if len(ips) > 0 {
			in.Ip = ips[0]
		}
	}

	var rsp *pb.LoginUserResp
	rsp, err = l.svcCtx.UcUserRpc.FbUserLogin(l.ctx, &in)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, err
	}
	resp = &types.UserLoginResp{
		Token:   &types.UserToken{},
		Account: &types.UserAccount{},
		Profile: &types.UserProfile{},
	}
	_ = copier.Copy(resp, rsp)
	return
}
